//
//  LZPlayViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZPlayViewController.h"
#import "FitHeader.h"
#import "UIImage+GaussianBlur.h"
#import "LZListOrHistroyViewController.h"
#import "LZPlayOrPauseTableViewCell.h"
#import "TwoToOne.h"
#import "LZBillboardModel.h"
#import "UIImageView+WebCache.h"
@interface LZPlayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSTimer *timer;
@end

@implementation LZPlayViewController
#pragma mark - 视图将要出现时
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _flag = 0;
    _dataSourceArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];
    
    _backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 540 * FITHeight)];
    _backImageView.image = [UIImage imageWithBlurImage:[UIImage imageNamed:@"高斯模糊"] intputRadius:20];
    _backImageView.userInteractionEnabled = YES;
    [self.view addSubview:_backImageView];
    
    _button = [BaseButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(20 * FITWidth, 30 * FITHeight, 30 * FITWidth, 30 * FITHeight);
    [_button setTintColor:[UIColor whiteColor]];
    [_button setImage:[UIImage imageNamed:@"下箭头"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    [_backImageView addSubview:_button];
    
    _titleLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(_button.frame.origin.x + _button.frame.size.width + 10 * FITWidth, _button.frame.origin.y, 250 * FITWidth, _button.frame.size.height)];
    _titleLabel.text = _lzBill.title;
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.textColor = [UIColor whiteColor];
    [_backImageView addSubview:_titleLabel];
    
    _playtimesLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x + _titleLabel.frame.size.width, _titleLabel.frame.origin.y, 50 * FITWidth, _titleLabel.frame.size.height)];
    _playtimesLabel.font = [UIFont systemFontOfSize:14];
    _playtimesLabel.textColor = [UIColor whiteColor];
    [_backImageView addSubview:_playtimesLabel];
    
    _coverMiddleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x + 20 * FITWidth, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 80 * FITHeight, 215 * FITWidth, 215 * FITHeight)];
    [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:_lzBill.coverMiddle] placeholderImage:[UIImage imageNamed:@""]];
    [_backImageView addSubview:_coverMiddleImageView];
    
    _downloadButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    _downloadButton.frame = CGRectMake(_coverMiddleImageView.frame.origin.x + _coverMiddleImageView.frame.size.width + 20 * FITWidth, _coverMiddleImageView.frame.origin.y + 50 * FITHeight, 32 * FITWidth, 32 * FITHeight);
    [_downloadButton setImage:[UIImage imageNamed:@"下载白色"] forState:UIControlStateNormal];
    [_downloadButton addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
    
    _shareButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    _shareButton.frame = CGRectMake(_downloadButton.frame.origin.x, _downloadButton.frame.origin.y + _downloadButton.frame.size.height + 40 * FITHeight, _downloadButton.frame.size.width, _downloadButton.frame.size.height);
    [_shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [_shareButton setImage:[UIImage imageNamed:@"分享白色"] forState:UIControlStateNormal];
    [_backImageView addSubview:_shareButton];
    
    [_backImageView addSubview:_downloadButton];
    
    _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progressView.frame = CGRectMake(_coverMiddleImageView.frame.origin.x - 20 * FITWidth, _coverMiddleImageView.frame.origin.y + _coverMiddleImageView.frame.size.height + 50 * FITHeight, _coverMiddleImageView.frame.size.width + 40 * FITWidth, 20);
    [_backImageView addSubview:_progressView];
    
    _listImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_button.frame.origin.x, _coverMiddleImageView.frame.origin.y + _coverMiddleImageView.frame.size.height + 110 * FITHeight, 32 * FITWidth, 32 * FITHeight)];
    _listImageView.image = [UIImage imageNamed:@"播放列表"];
    _listImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *listTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(listAction)];
    [_listImageView addGestureRecognizer:listTap];
    [_backImageView addSubview:_listImageView];
    
    _goBackImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 20 * FITWidth, _listImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _goBackImageView.image = [UIImage imageNamed:@"上一首"];
    [_backImageView addSubview:_goBackImageView];
    
    _playOrPauseImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 85 * FITWidth, _goBackImageView.frame.origin.y - 10 * FITHeight, 48 * FITWidth, 48 * FITHeight)];
    _playOrPauseImageView.image = [UIImage imageNamed:@"播放白色"];
    [_backImageView addSubview:_playOrPauseImageView];
    
    _goNextImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + _coverMiddleImageView.frame.size.width - 52 * FITWidth, _goBackImageView.frame.origin.y, _goBackImageView.frame.size.width, _goBackImageView.frame.size.height)];
    _goNextImageView.image = [UIImage imageNamed:@"下一首"];
    [_backImageView addSubview:_goNextImageView];
    
    _historyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_playtimesLabel.frame.origin.x, _goBackImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _historyImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *historyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(historyAction)];
    [_historyImageView addGestureRecognizer:historyTap];
    _historyImageView.image = [UIImage imageNamed:@"播放历史"];
    [_backImageView addSubview:_historyImageView];
    
    _tableView.tableHeaderView = _backImageView;
    
    //创建一个计时器使之自动循环播放
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    
    [self getData];
}
- (void)timerAction{
    CGRect frame = CGRectMake(_button.frame.origin.x + _button.frame.size.width + 10 * FITWidth, _button.frame.origin.y, 550 * FITWidth, _button.frame.size.height);
    frame.origin.x = 320 * FITWidth;
    self.titleLabel.frame = frame;
    [UIView beginAnimations:@"testAnimation"context:NULL];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    frame = self.titleLabel.frame;
    frame.origin.x = - _titleLabel.frame.size.width;
    self.titleLabel.frame = frame;
    [UIView commitAnimations];
}
- (void)getData{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=android&trackId=%@", _lzBill.trackId];
    [TwoToOne getDataByString:urlString BodyString:nil WithDataBlock:^(id data) {
        LZBillboardModel *lzBil = [[LZBillboardModel alloc]init];
        [lzBil setValuesForKeysWithDictionary:data];
        [_dataSourceArray addObject:lzBil];
        [_tableView reloadData];
    }];
}
#pragma mark - 下载
- (void)downloadAction{
    NSLog(@"下载");
    if (_flag == 0) {
        [_downloadButton setImage:[UIImage imageNamed:@"对号白色"] forState:UIControlStateNormal];
    }
}
#pragma mark - 分享
- (void)shareAction{
    NSLog(@"分享");
}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90 * FITHeight;
}
#pragma mark - cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}
#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    LZPlayOrPauseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[LZPlayOrPauseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = 0;
    LZBillboardModel *lzBill = _dataSourceArray[indexPath.row];
    [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:lzBill.coverLarge] placeholderImage:[UIImage imageNamed:@""]];
    _playtimesLabel.text = [NSString stringWithFormat:@"%.1f万", [lzBill.playtimes integerValue] / 10000.0];
    cell.lzBill = lzBill;
    
    return cell;
}
#pragma mark - 播放列表
- (void)listAction{
    LZListOrHistroyViewController *lzList = [[LZListOrHistroyViewController alloc]init];
    lzList.titleString = @"播放列表";
    [self.navigationController pushViewController:lzList animated:YES];
}
#pragma mark - 播放历史
- (void)historyAction{
    LZListOrHistroyViewController *lzListOrHistroy = [[LZListOrHistroyViewController alloc]init];
    lzListOrHistroy.titleString = @"播放历史";
    
    [self.navigationController pushViewController:lzListOrHistroy animated:YES];
}
- (void)dismissAction{
    [UIView animateWithDuration:2 animations:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}
+ (LZPlayViewController *)shareInstance{
    static LZPlayViewController *lzPlay = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lzPlay = [[LZPlayViewController alloc]init];
    });
    return lzPlay;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
